package com.rtx.wms.std.handler.so.finish;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.wms.std.common.bean.WmsConstant;
import com.rtx.wms.std.so.bean.ApiOrderdetailfinish;
import com.rtx.wms.std.so.bean.ApiOrdersfinish;
import com.rtx.wms.std.so.bean.Orderdetail;
import com.rtx.wms.std.so.bean.Orders;
import com.rtx.wms.std.so.service.ApiOrdersfinishService;
import com.rtx.wms.std.so.service.OrdersService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

@Component
public class OrdersFinishHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private ApiOrdersfinishService apiOrdersfinishService;

    /**
     * 处理完成的出库单
     * @param whseid
     */
    public void handlerOrdersFinish(String whseid){
        //获取待回传的出库单
        List<Orders> ordersList = selectOrders(whseid);
        for(Orders o : ordersList){
            //写入回传中间表
            addApiOrdersfinish("ris_edi",o);
            //更新状态
            updateOrders(whseid,o);
        }
    }

    /**
     * 写入回传中间表
     * @param whseid
     * @param o
     */
    private void addApiOrdersfinish(String whseid, Orders o) {
        try {
            ApiOrdersfinish apiOrdersfinish = convertApiOrdersfinish(o);
            apiOrdersfinishService.addApiOrdersfinish(whseid,apiOrdersfinish);
            o.setEdiflag(WmsConstant.SUCCESS_FLAG);
        } catch (Exception e) {
            logger.error("仓库：{}，出库单号：{}写入回传中间表异常",whseid,o.getOrderkey(),e);
            o.setEdiflag(WmsConstant.FAIL_FLAG);
            o.setEdimsg(e.getMessage());
        }
    }

    /**
     * 转化类型
     * @param o
     * @return
     */
    private ApiOrdersfinish convertApiOrdersfinish(Orders o) {
        //获取唯一编号
        String batchid = IdUtil.getSnowflakeNextIdStr();
        ApiOrdersfinish apiOrdersfinish = new ApiOrdersfinish();
        BeanUtils.copyProperties(o,apiOrdersfinish,"details");
        apiOrdersfinish.setRtxqueue(0);
        apiOrdersfinish.setEdiflag("P");
        List<ApiOrderdetailfinish> details = new ArrayList<>();
        for(Orderdetail od : o.getDetails()){
            ApiOrderdetailfinish detail = new ApiOrderdetailfinish();
            BeanUtils.copyProperties(od,detail);
            detail.setBatchid(batchid);
            details.add(detail);
        }
        
        apiOrdersfinish.setBatchid(batchid);
        apiOrdersfinish.setDetails(details);
        
        try {
        	apiOrdersfinish.setActualshipdate(DateUtil.format(o.getActualshipdate(), "yyyy-MM-dd"));
		} catch (Exception e) {
		}
        return apiOrdersfinish;
    }

    
    /**
     * 更新出库单
     * @param whseid
     * @param o
     */
    private void updateOrders(String whseid, Orders o) {
        LambdaUpdateWrapper<Orders> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(Orders::getOrderkey,o.getOrderkey())
                .set(Orders::getEdiflag,o.getEdiflag())
                .set(Orders::getEdimsg, StrUtil.sub(o.getEdimsg(),0,200));
        ordersService.updateByWrapper(whseid,wrapper);
    }

    /**
     * 查询需要回传上位出库单
     * @param whseid
     * @return
     */
    public List<Orders> selectOrders(String whseid){
        return ordersService.selectFinishOrdersByWrapper(whseid);
    }
}
